package com.example.shopping2.a_c_fragment.car

import BaseActivity
import IActivity
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.LjlUtils

class Add_Address_Activity : BaseActivity<HomePresenter>(), IActivity, HomeContract.HomeView {
    private val addAddressBack: ImageView by lazy { findViewById<ImageView>(R.id.add_address_back) }
    private val mShipNameEt: EditText by lazy { findViewById<EditText>(R.id.mShipNameEt) }
    private val mShipMobileEt: EditText by lazy { findViewById<EditText>(R.id.mShipMobileEt) }
    private val mShipAddressEt: EditText by lazy { findViewById<EditText>(R.id.mShipAddressEt) }
    private val mSaveBtn: AppCompatButton by lazy { findViewById<AppCompatButton>(R.id.mSaveBtn) }
    private var userid: Long? = 0
    private var floag = 0
    private var id = 0
    override fun findlayout(): Int {
        return R.layout.activity_add_address
    }

    override fun initView() {
        userid =
            getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE)
                ?.getLong("id", 1)

        var name = intent.getStringExtra("name")
        var address = intent.getStringExtra("address")
        var mobile = intent.getStringExtra("mobile")
         id = intent.getIntExtra("id",0)
         floag = intent.getIntExtra("floag",0)
        mShipNameEt.setText(name)
        mShipMobileEt.setText(mobile)
        mShipAddressEt.setText(address)

    }

    override fun initData() {
        mypre = HomePresenter(HomeModel(), this)
        mSaveBtn.setOnClickListener {
            if (floag == 0){
                var name = mShipNameEt.text.toString()
                var mobile = mShipMobileEt.text.toString()
                var address = mShipAddressEt.text.toString()

                //获取订单
                var list: ArrayList<HashMap<String, Any>> = ArrayList()
                var map: HashMap<String, Any> = HashMap()
                map.put("code", HttpCode.ADDADDRESS)
                map.put("shipAddress", address)
                map.put("shipIsDefault", 1)
                map.put("shipUserMobile", mobile)
                map.put("shipUserName", name)
                map.put("userId", userid!!)
                list.add(map)
                mypre.requestAll(list)
            }else{
                var name = mShipNameEt.text.toString()
                var mobile = mShipMobileEt.text.toString()
                var address = mShipAddressEt.text.toString()
                //获取订单
                var list: ArrayList<HashMap<String, Any>> = ArrayList()
                var map: HashMap<String, Any> = HashMap()
                map.put("code", HttpCode.EDIADDRESS)
                map.put("shipAddress", address)
                map.put("shipIsDefault", 1)
                map.put("shipUserMobile", mobile)
                map.put("shipUserName", name)
                map.put("userId", userid!!)
                map.put("id", id)
                list.add(map)
                mypre.requestAll(list)
            }




        }
    }

    override fun showHomeData(baseEntity: BaseEntity) {
        if (baseEntity.status.equals("0")) {
            LjlUtils.Ljl_Toast(this,""+baseEntity.message)
            finish()


        }
    }

}